const db = require('../../lib/conn_oracle')

exports.getJobInfo = async (req,res)=>{
    let {form} = req.body
    let {status,desc} = form
    // console.log(req.body);
    // console.log(date);
    
    let resDatas     
    let statusWhere = status === "" ? "" : `and status = '${status}'`
    let descWhere = desc === "" ? "" : `and description like '%${desc}%'`
    let handleSql = `
                     select * from c##wxw.job
                     where 1 = 1
                       ${statusWhere}
                       ${descWhere}
                       order by b_type,s_type
                    `
    let workTypeSql = `select * from c##wxw.worktype order by b_type,s_type`
    let jobInfo = await db.search2(handleSql)
    let workTypeInfo = await db.search2(workTypeSql)
    resDatas = {jobInfo,workTypeInfo}
    res.json({
        status:1,
        msg:"查询成功",
        resDatas
    })
}

exports.editJob = async (req,res)=>{
    // console.log(req.body);
    let {type,form} = req.body
    let {JOBID,DESCRIPTION,STATUS,B_TYPE,S_TYPE,LEADTIME,NEXTDATE,FREQUENCY,FREQUNIT,REMARK,W_STATUS} = form
    let resDatas
    let msg
    let status
    let submitTypeStr = type === "add" ? "新增" : "更新"
    let handleSql = type === "add" ? `
                                      insert into c##wxw.job(jobid,description,status,b_type,s_type,leadtime,nextdate,frequency,frequnit,remark,w_status)
                                      values('',:DESCRIPTION,:STATUS,:B_TYPE,:S_TYPE,:LEADTIME,to_timestamp(:NEXTDATE,'yyyy-mm-dd hh24:mi:ss.ff'),:FREQUENCY,:FREQUNIT,:REMARK,:W_STATUS)
                                     `
                                    :  `
                                        update c##wxw.job 
                                        set
                                        description = :DESCRIPTION ,
                                        status = :STATUS,
                                        b_type = :B_TYPE,
                                        s_type = :S_TYPE,
                                        leadtime = :LEADTIME,
                                        nextdate = to_timestamp(:NEXTDATE,'yyyy-mm-dd hh24:mi:ss.ff'),
                                        frequency = :FREQUENCY,
                                        frequnit = :FREQUNIT,
                                        remark =  :REMARK,
                                        w_status = :W_STATUS
                                        where jobid = :JOBID
                                        `
    let handleDate = type === "add" ? [DESCRIPTION,STATUS,B_TYPE,S_TYPE,LEADTIME,NEXTDATE,FREQUENCY,FREQUNIT,REMARK,W_STATUS] 
                                    : [DESCRIPTION,STATUS,B_TYPE,S_TYPE,LEADTIME,NEXTDATE,FREQUENCY,FREQUNIT,REMARK,W_STATUS,JOBID]
    // console.log(  handleSql  );
    
    let newJob = await db.writeTab(handleSql,handleDate)
    resDatas = {newJob}
    msg = resDatas.newJob.errorNum ? `${submitTypeStr}失败，错误代码：ORA-` + resDatas.newJob : `${submitTypeStr}成功`
    status = resDatas.newJob.errorNum ? 0 : 1
    res.json({
        status,
        msg,
        resDatas
    })                                                           
}